package com.hjy.order.web;

import cn.hutool.core.util.RandomUtil;
import com.hjy.order.entity.OrderEntity;
import com.hjy.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;

@RestController
public class TestController {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("/send")
    public String test1() {
        OrderReturnReasonEntity orderReturnReasonEntity = new OrderReturnReasonEntity();

        orderReturnReasonEntity.setId(1L);
        orderReturnReasonEntity.setCreateTime(new Date(System.currentTimeMillis()));
        orderReturnReasonEntity.setName("你好");

        // 发送信息
        /**
         * String exchange : 发送到哪个交换机
         * String routingKey : 路由 key
         * final Object object : 发送的信息
         */
        int i = RandomUtil.randomInt(2);
        if (i % 2 == 0) {
            rabbitTemplate.convertAndSend("hello.java.exchange.direct",
                    "hello.java.queue1",
                    orderReturnReasonEntity);
        }else {
            rabbitTemplate.convertAndSend("hello.java.exchange.direct",
                    "hello.java.queue1",
                    new OrderEntity());
        }

        return "OK";
    }
}
